#include "ColorCheckBrowser.hpp"
#include "MeshDraw.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//#include "FL/flstring.h"
#include <FL/fl_draw.H>
#include <boost/lexical_cast.hpp>

#define CHECK_SIZE (textsize()-2)

std::string ColorCheckBrowser::RegionLabel(int aRegion)
{
   int r = aRegion;
   std::string lbl("Region " + boost::lexical_cast<std::string>(r));
   unsigned int col = fl_rgb_color(MeshDraw::sMaterialColors[r][0]*255, MeshDraw::sMaterialColors[r][1]*255, MeshDraw::sMaterialColors[r][2]*255);
   lbl = "@C"
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r][0]) + ","
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r][1]) + ","
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r][2]) + "/"
      + lbl;
   return lbl;
}

std::string ColorCheckBrowser::RegionLabel(int r1, int r2)
{
   std::string lbl = boost::lexical_cast<std::string>(r1) + "      -      " + boost::lexical_cast<std::string>(r2);
   //unsigned int col = fl_rgb_color(MeshDraw::sMaterialColors[r1][0]*255, MeshDraw::sMaterialColors[r1][1]*255, MeshDraw::sMaterialColors[r1][2]*255);
   lbl = "@C"
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r1][0]) + ","
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r1][1]) + ","
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r1][2]) + "/"
      + "@C"
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r2][0]) + ","
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r2][1]) + ","
      + boost::lexical_cast<std::string>(MeshDraw::sMaterialColors[r2][2]) + "/"
      + lbl;
   return lbl;
}

void ColorCheckBrowser::item_draw(void *v, int X, int Y, int, int) const
{
   cb_item *i = (cb_item *)v;
   char *s = i->text;
   int tsize = textsize();
   Fl_Color col = active_r() ? textcolor() : fl_inactive(textcolor());
   int cy = Y + (tsize + 1 - CHECK_SIZE) / 2;
   X += 2;


   bool colChange(false);
   char* sOrig = s;
   Fl_Color formatCols[2];
   int formatColCount=0;
   if (s)
   {
      while (*s == '@')
      {
         char c = *++s;
         if (c == 'C')
         {
            ++s;
            float r,g,b;
            sscanf(s, "%f,%f,%f/", &r,&g,&b);
            while (*s != '/') ++s;
            ++s;
            formatCols[formatColCount++] = fl_rgb_color(r,g,b);
            if (formatColCount == 1)
            {
               formatCols[1] = formatCols[0];
            }
            if (formatColCount == 2) continue;
         }
      }
   }

   fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR));
   fl_loop(X, cy, X, cy + CHECK_SIZE,
      X + CHECK_SIZE, cy + CHECK_SIZE, X + CHECK_SIZE, cy);

   if (i->checked) {
      int tx = X + 3;
      int tw = CHECK_SIZE - 4;
      int d1 = tw/3;
      int d2 = tw-d1;
      int ty = cy + (CHECK_SIZE+d2)/2-d1-2;
      for (int n = 0; n < 3; n++, ty++) {
         fl_line(tx, ty, tx+d1, ty+d1);
         fl_line(tx+d1, ty+d1, tx+tw-1, ty+d1-d2+1);
      }
   }

   col = formatCols[1];
   {
      int left = X + CHECK_SIZE + 5;
      //int mid = (left + w() ) / 2;
      int wid=50;
      int mid = wid + left;
      int right = left + wid * 2;
      fl_color(formatCols[1]);
      fl_rectf(mid, cy, wid, tsize);
      fl_color(formatCols[0]);
      fl_rectf(left, cy, mid-left, tsize);
   }

   fl_font(textfont(), tsize);
   //if (i->selected) {
      col = fl_contrast(FL_WHITE, col);//selection_color());
   //}
   
   fl_color(col);
   //fl_color(FL_WHITE);
   fl_draw(s, X + CHECK_SIZE + 8, Y + tsize - 1);
}